﻿using Microsoft.AspNetCore.Http;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lg.Core
{
    public class Tools
    {
        public static string MapPath(string path)
        {
            path = AppContext.BaseDirectory + path.Replace("/", "\\");
            return path.Replace("\\\\", "\\");
        }
        /// <summary>
        /// 获取由guid生成的数字组合字符串，可用于唯一识别号
        /// </summary>
        /// <returns></returns>
        public static string GetSN()
        {
            return BitConverter.ToInt64(Guid.NewGuid().ToByteArray()).ToString();
        }
        /// <summary>
        /// 获取当前网址，根据urlFormat进行格式化并返回
        /// 关键字含protocol、host、path、query
        /// </summary>
        /// <param name="request"></param>
        /// <param name="urlFormat"></param>
        /// <returns></returns>
        public static string GetUrl(HttpRequest request, string urlFormat)
        {
            string protocol = request.Scheme;  // 获取协议类型，例如：http 或 https
            string host = request.Host.Value;  // 获取主机名和端口号，例如：www.example.com:80
            string path = request.Path;        // 获取请求路径，例如：/some/path
            string query = request.QueryString.Value??"";  // 获取查询字符串，例如：?key1=value1&key2=value2
            return urlFormat.ToLower().Replace("{protocol}",protocol).Replace("{host}",host).Replace("{path}",path).Replace("{query}",query);
        }
    }
}
